package com.dashu.device.service.JL;

import com.alibaba.fastjson.JSONObject;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.date.TimeUtils;
import com.dashu.device.modelat.Distinguish;
import com.dashu.device.api.JL.JLInsideServerApi;
import com.dashu.device.bean.JL.JLComparedBean;
import com.dashu.device.bean.JL.JLHeartBean;
import com.dashu.device.cache.CommomLocalCache;
import com.dashu.device.service.*;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;


@Slf4j
@Service
public class JLInsideInsideService<T> extends AttendanceBaseServer implements JLInsideServerApi<T> {
    @Autowired
    private DeviceService deviceService;

    @Override
    public void heartbeat(ChannelHandlerContext ctx, Object object, long cacheTime) throws Exception {
        JLHeartBean jlHeartBean = CopyUtil.copyEnty(object, JLHeartBean.class);
        deviceService.updataByDeviceUUId(jlHeartBean.getDeviceUUID(),null);
        System.out.println(jlHeartBean);
        CommomLocalCache.putTime(jlHeartBean.getDeviceUUID(), jlHeartBean, cacheTime);
    }

    @Override
    public void compared(ChannelHandlerContext ctx, Object o, String msg) throws IOException {
        JSONObject userJson = JSONObject.parseObject(msg);
        log.info(String.valueOf(userJson));
        Distinguish distinguish = new Distinguish();
        JLComparedBean jlComparedBean = CopyUtil.copyEnty(userJson, JLComparedBean.class);
        List<String> pg = Arrays.asList(jlComparedBean.getPicName().split("_"));
        if (pg.size() > 1) {
            distinguish.setId(UIDUtil.nextId());
            distinguish.setName(pg.get(0));
            distinguish.setIden(pg.get(1));
            distinguish.setCId(Long.valueOf(pg.get(2).substring(0, pg.get(2).indexOf("."))));
            distinguish.setImg(imgRecord(jlComparedBean.getImg(), pg.get(2)));
            distinguish.setCaptureTime(TimeUtils.stringTostamp(jlComparedBean.getCaptureTime()));
            distinguish.setCompareResult(Double.parseDouble(jlComparedBean.getCompareResult()));
            distinguish.setUuid(jlComparedBean.getDeviceUUID());
            distinguish.setTemperature(Double.parseDouble(jlComparedBean.getTemperature()));
            isAttendance(distinguish);
        }
    }





}

